home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / ensoft11.zip / ENSOFT11.C next >
Text File  |  1986-01-01  |  3KB  |  140 lines

  1. /************************************************************************/
  2. /* Program in Small-C to convert a regular text file to Wordstar    */
  3. /*   'document' format with soft spaces and carriage returns.        */
  4. /*                                    */
  5. /*    by Gordon Brandly, R.R.2, Fort Sask., AB, CANADA  T8L 2N8    */
  6. /*                                    */
  7. /*    converted for use with Software Toolworks C/80            */
  8. /*    & added command line parameter passing                */
  9. /*        - paul homchick 3/25/84                    */
  10. /*    compiled for DOS using CI C86                    */
  11. /*        - paul homchick 1/1/86                    */
  12. /************************************************************************/
  13.  
  14. #include "stdio.h"
  15.  
  16. #define VERSION    "1.1C"
  17. #define VDATE    "1 Jan 86"
  18. #define CR    0x0d
  19. #define LF    0x0a
  20. #define TAB    0x09
  21. #define CPM_EOF 0x1a
  22. #define TRUE    1
  23. #define FALSE    0
  24. #define ERROR    -1
  25.  
  26. int startline, inword, inptr, outptr;
  27.  
  28. main(argc,argv)
  29. int argc; char *argv[];
  30. {
  31.     int c;
  32.     startline=TRUE;
  33.     inword=FALSE;
  34.     if(argc < 3)
  35.     {
  36.         printf("ensoft version %s %s\n",VERSION,VDATE);
  37.         printf("usage: ensoft ascii_input_name");
  38.         printf(" wordstar_output_name\n");
  39.         printf("converts text files to wordstar document format.\n");
  40.         exit(0);
  41.     }
  42.  
  43.     if(strcmp(argv[1],argv[2])==0)
  44.     {
  45.         printf("Input and output filenames must differ.");
  46.         printf("  Aborting...\007\n");
  47.         exit(0);
  48.     }
  49.  
  50.     if(!(inptr= fopen(argv[1],"r")))
  51.     {
  52.         printf("Can't open '%s'",argv[1]);
  53.         printf(" for input.\n");
  54.         exit(0);
  55.     }
  56.  
  57.     if(!(outptr=fopen(argv[2],"wb")))
  58.     {
  59.         printf("Can't open '%s'",argv[2]);
  60.         printf(" for output. The disk directory is probably full.\n");
  61.         exit(0);
  62.     }
  63.  
  64. /************************************************************************/
  65. /*                 main loop                */
  66. /************************************************************************/
  67.  
  68.     printf("processing... ");
  69.     while ((c=getc(inptr))!=ERROR && c!=CPM_EOF)
  70.         putc(translate(c),outptr);
  71.     putc(CPM_EOF,outptr);
  72.     fclose(inptr);
  73.     fclose(outptr);
  74.     printf("done.\n");
  75. }
  76.  
  77. /************************************************************************/
  78. /*            wordstar translation routine            */
  79. /************************************************************************/
  80.  
  81. translate(c)
  82. int c;
  83. {
  84.     if (c==LF)
  85.     {
  86.         if (startline)
  87.         {
  88.             putc(CR,outptr);
  89.             return(c);    /* empty lines are 'hard' */
  90.         }
  91.         else
  92.         {
  93.             c=getc(inptr);
  94.             if ((c==' ') || (c==TAB)) /* lines beginning    */
  95.             {        /* with a space or TAB probably */
  96.                     /* begin a new paragraph    */
  97.                  putc(CR,outptr);
  98.                 putc(LF,outptr);
  99.             }
  100.             else if ((c==LF) || (c==(-1)) || (c==CPM_EOF))
  101.                  {
  102.                 putc(CR,outptr);   /* end of a paragraph? */
  103.                 putc(LF,outptr);
  104.                 startline=TRUE;
  105.                 return(translate(c));
  106.                  }
  107.             else
  108.             {
  109.                 putc((CR | 0x80),outptr);
  110.                     putc(LF,outptr);
  111.             }
  112.             startline=TRUE;
  113.             return(translate(c));
  114.         }
  115.     }
  116.     else if (c==' ')
  117.     {
  118.         if (inword)
  119.         {
  120.             inword=FALSE;    /* we just finished a word,     */
  121.             return(c);    /* so this is a 'hard' space     */
  122.         }
  123.         if (startline)
  124.             return(c);    /* spaces at the beginning of     */
  125.                     /* a line are also 'hard' spaces */
  126.         return(c | 0x80);    /* otherwise, a 'soft' space     */
  127.     }
  128.     else if (c==(-1)||c==CPM_EOF)
  129.     {
  130.         return(CPM_EOF);
  131.     }
  132.     else
  133.     {
  134.         inword=TRUE;        /* any other character counts as */
  135.         startline=FALSE;    /* a word.             */
  136.         return(c);
  137.     }
  138.  
  139. }
  140.